#include<mylib/mylib.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>


void mergesort(int* arr,int* tmp,int left,int right)
{
    if(left>=right) return;
    int mid=left+(right-left)/2;
    mergesort(arr,tmp,left,mid);
    mergesort(arr,tmp,mid+1,right);
    int begin1=left,end1=mid,begin2=mid+1,end2=right,i=left;
    while(begin1<=end1&&begin2<=end2)
    {
        if(arr[begin1]<arr[begin2]) tmp[i++]=arr[begin1++];
        else tmp[i++]=arr[begin2++];
    }
    while(begin1<=end1) tmp[i++]=arr[begin1++];
    while(begin2<=end2) tmp[i++]=arr[begin2++];
    memcpy(arr+left,tmp+left,sizeof(int)*(end2-left+1));
}
int main()
{
    int arr[10]={10,9,8,7,6,5,4,3,2,1};
    int* tmp=(int*)malloc(sizeof(int)*10);
    mergesort(arr,tmp,0,9);
    int i=0;
    for(i=0;i<10;i++) printf("%d ",arr[i]);
    return 0;
}
